#include "ImageRecognize.h"


ImageRecognize::ImageRecognize(const QStringList &argumet, QObject *parent)
    :QThread(parent)
{
    arguments = argumet;
}

ImageRecognize::~ImageRecognize()
{

}

void ImageRecognize::setfileinfo(const QFileInfo &fileinfo)
{
    fileInfo = fileinfo;
}

void ImageRecognize::recognize()//const QString &filePath
{

        qDebug()<<"当前线程"<<QThread::currentThread();

        QByteArray result = llm->connectAliyun(arguments);
        qDebug()<<result;
        QString content = llm->pocessJson(result);
        QJsonObject res = llm->parseJsonData(content);
        //返回content提示词
        QString contents = llm->content(res);

        qDebug()<<contents;

        QStringList argument;

        argument <<"sql.py" << "--content"<<contents;

        QByteArray sqlresult = llm->connectAliyun(argument);

        //qDebug()<<"阿里云返回的数据"<<result;

        QString sqlcontent = llm->pocessJson(sqlresult);
        qDebug()<< sqlcontent;

        sqlcontent = llm->sql(sqlcontent);

        qDebug()<<"sql语句为:"<< sqlcontent;

        //连接数据库
        MySql * mysql = new MySql();
        mysql->setfileInfo(fileInfo);
        QSqlDatabase db = mysql->connectSql();
        // 打开数据库连接
        if (!db.open()) {
            QSqlError err = db.lastError();
        }
        else
            qDebug() << "数据库连接成功!";
        mysql->ExecSql(sqlcontent);
        //关闭数据库
        db.close();
//        mysql->setbuyer_name(res["seller_name"].toString());
//        mysql->setseller_name(res["buyer_name"].toString());
//        mysql->setinvoice_date(res["invoice_date"].toString());
//        mysql->settax_amount(res["tax_amount"].toDouble());
//        mysql->settotal_amount(res["tax_amount"].toDouble());
//        mysql->setinvoice_number(res["invoice_number"].toString());
        //将返回的处理后显示到空白处.
        QString str = ("销售方姓名为:"+res["seller_name"].toString());
        str.append("\n");
        str.append("购买方姓名为:"+res["buyer_name"].toString());
        str.append("\n");
        str.append("开票日期为:"+res["invoice_date"].toString());
        str.append("\n");
        float total_amount = res["total_amount"].toDouble();
        str.append("总金额为:"+QString::number(total_amount));
        str.append("\n");
        float tax_amount = res["tax_amount"].toDouble();
        str.append("税额为:"+QString::number(tax_amount));
        str.append("\n");
        str.append("发票号码为:"+res["invoice_number"].toString());

        qDebug()<<"识别结果为:"<<str;
        emit Finshed(str);
}

void ImageRecognize::statis()
{

    QStandardItemModel *table = new QStandardItemModel();

    QByteArray sqlresult = llm->connectAliyun(arguments);

    //qDebug()<<"1"<<sqlresult;

    QString output = QString::fromLocal8Bit(sqlresult);
    QJsonDocument res = QJsonDocument::fromJson(output.toUtf8());
    QJsonObject jsonObject = res.object();
    QJsonArray choices = jsonObject.value("choices").toArray();
    QJsonObject choice = choices.at(0).toObject();
    QJsonObject message = choice.value("message").toObject();
    QString content = message.value("content").toString().replace("\\n"," ");

    content.replace("```","");

    content.replace("sql","");



    qDebug()<<"sql语句为:"<< content;

    MySql * mysql = new MySql();
    //返回content提示词
    //连接数据库
    QSqlDatabase db = mysql->connectSql();
    // 打开数据库连接
    if (!db.open()) {
        QSqlError err = db.lastError();
    }
    else
        qDebug() << "数据库连接成功!";
    QSqlQueryModel model;

    model.setQuery(content);

    // 获取列数和行数
    int columnCount = model.columnCount();
    int rowCount = model.rowCount();

    QStringList headers;

    // 获取表头
    for (int i = 0; i < columnCount; i++) {
        headers.append(model.headerData(i, Qt::Horizontal).toString());
        qDebug() << "Column" << i << "name:" << model.headerData(i, Qt::Horizontal).toString();
    }
    qDebug()<<headers;
    //设置表头
    table->setHorizontalHeaderLabels(headers);

    // 遍历所有行和列的数据
    for (int row = 0; row < rowCount; row++) {
        QList<QStandardItem*> rowItems;
        for (int col = 0; col < columnCount; col++) {
            QModelIndex index = model.index(row, col);
            rowItems.append(new QStandardItem(model.data(index).toString()));

            //每一行中每一列的值
            QString value = model.data(index).toString();
            QString columnName  = model.headerData(col, Qt::Horizontal).toString();
            qDebug() << "Row:" << row << "Column:" << columnName << "Value:" << value;
        }
        table->appendRow(rowItems);
        qDebug() << "---"; // 行分隔符
    }
    //关闭数据库
    db.close();
    emit FinshedQuery(table);
}



